<?php
/**
 * 系統名稱: Lotto 系統
 * 檔案說明: 專案管理-修改
 * $Author$
 * $Id$
 *
 */
if (!defined("Commission_lib_include")){
    define("Commission_lib_include", 1);

/**
 * 取得使用者退水陣列
 * @param	intUserID 使用者序號
 * @param	intLID 樂透種類序號
 * @return	錯誤時傳回 false 程式終止，正確傳回 Array
 */
	function getUserCommissionArray($intUserID, $intLID = 0){
		global $_Conn, $$_Conn;
		$arrComm = array();
		$strSQL = "select LottoID, Bet_Type, Gamble_Type, Commission from `bet_Commission` where MemID = ".$intUserID." and Gamble_Type < 10";
		if($intLID > 0)$strSQL .= " and LottoID = ".$intLID;
		$strSQL .= " order by Gamble_Type";
		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Game Limit Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrComm[$Rows -> LottoID][$Rows -> Bet_Type][$Rows -> Gamble_Type] = $Rows -> Commission;
		}
		return $arrComm;
	}


/**
 * 取得使用者退水轉賠率陣列
 * @param	intUserID 使用者序號
 * @param	intLID 樂透種類序號
 * @param	arrBT 下注種類陣列
 * @return	錯誤時傳回 false 程式終止，正確傳回 Array
 */
	function getUserCommToOdds($intUserID, $intLID, $arrBT){
		global $_Conn, $$_Conn;
		$arrComm = array();
		$strSQL = "select LottoID, Bet_Type, ApointNum, Gamble_Type, Commission from `bet_Commission` where MemID = ".$intUserID." and LottoID = ".$intLID." and Gamble_Type > 10 and Gamble_Type < 20";
		if(is_array($arrBT)){
			if(sizeof($arrBT) > 0){
				$strSQL .= " and Bet_Type in (".implode(", ", $arrBT).")";
			}
		}
		$strSQL .= " order by Gamble_Type";

		if(!$RS = @PMA_mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Game Limit Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".PMA_mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}
		while($Rows = PMA_mysql_fetch_object($RS)){
			$arrComm[$Rows -> Bet_Type][$Rows -> ApointNum][$Rows -> Gamble_Type] = $Rows -> Commission;
		}
		return $arrComm;
	}


/**
 * 更改使用者退水轉賠率值
 * @param	intChangeUID 更改者序號
 * @param	intUID 使用者序號
 * @param	intLID 樂透序號
 * @param	intBT 下注種類
 * @param	intAN 指定
 * @param	intGLine 分盤種類
 * @param	flComm 退水值
 * @return	成功 true, 失敗 false
 */
	function setUserCommToOdds($intChangeUID, $intUID, $intLID, $intBT, $intAN, $intGLine, $flComm){
		global $_ConnMaster;

		//先將  ApointNumber 存在 MainScale 裡
		$strSQL = "insert `bet_RateChangeRecord`(ChangeUserID, UserID, LogType, LottoID, Bet_Type, MainScale, Scale, NewScale, ChangeDate, IP) select ".$intChangeUID.", MemID as UserID, Gamble_Type + 20, LottoID, Bet_Type, ApointNum, Commission, '".$flComm."', NOW(), '".$_SERVER["REMOTE_ADDR"]."'  from `bet_Commission` where MemID in (".$intUID.") and Bet_Type = ".$intBT." and LottoID = ".$intLID." and Gamble_Type = ".$intGLine." and Commission != '".$flComm."'";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update CommToOdds Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者退水轉賠率值錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}

		$strSQL = "insert into `bet_Commission`(`LottoID`, `Bet_Type`, `ApointNum`, `Gamble_Type`, `MemID`, `Commission`) values(".$intLID.", ".$intBT.", ".$intAN.", ".$intGLine.", ".$intUID.", ".$flComm.") ON DUPLICATE KEY UPDATE `Commission` = VALUES(`Commission`)";

		//$strSQL = "update `bet_Commission` set Commission = ".$flComm." where MemID in (".$strUID.") and Bet_Type = ".$intBT." and LottoID = ".$intLID." and Gamble_Type = ".$intGLine;
		//print $strSQL."<br>\n";
		if(!PMA_mysql_query($strSQL, $_ConnMaster)){
			$GLOBALS["errormessage"] = "Update CommToOdds Data Error!!";
			$GLOBALS["errorhidden"] = "\nError: ".PMA_mysql_error($_ConnMaster).",\nSQL: ".$strSQL.",\nLinue:".__LINE__.",\nPage:".__FILE__;
			AddData_Log($intChangeUID, "更改使用者退水轉賠率值錯誤", $GLOBALS["errorhidden"].$strSQL);
			return false;
		}
		return true;
	}


/**
 * 檢查會員分盤退水資料是否存在
 * @param	intUserID 使用者序號
 * @param	intBall 球類序號
 * @return	錯誤時傳回 false 程式終止，正確傳回 Array
 */
	function isExistMemberCommission($intMID, $intGLine){
		global $_Conn, $$_Conn;
		$arrComm = array();
		$strSQL = "select ID from `bet_Commission` where MemID = ".$intMID." and Gamble_Type = ".$intGLine;
		if(!$RS = @mysql_query($strSQL, $$_Conn)){
			$GLOBALS["errormessage"] = "Find Game Limit Data Error!!";
			$GLOBALS["errorhidden"] = "Error: ".mysql_error($$_Conn)."\nLine:".__LINE__."\nPage:".__FILE__;
			return false;
		}

		if(mysql_num_rows($RS) == 0)return 0;
		return 1;
	}
//-------------------------------------------------------//
//Commission_lib End
}
?>